import { AuthRouteWhites } from '@/router'
import ls from '@/lib/ls'
import router from '@/router/index.js'
import Api from '@/api'
import useConfigStore from '@/store/config'

export default {
    // 启动应用
    async Start(route){
        const token = ls.get('TOKEN')
        try{
            //本地有token存在
            if (!!token===true) {
                await this.Init()
            }else{
                //当前页面在认证白名单内
                if (AuthRouteWhites.includes(route.path)){
                    this.completed = true
                    return
                }
                throw new Error('没有token')
            }
        }catch(err){
            console.error(err)
            ls.remove('TOKEN')
        }
    },
	// 初始化应用
    async Init(){
        const configStore = useConfigStore()
        // try {
        // } catch (error) {
        //     console.log('error init')
        // }
        await configStore.UpdateAll()
        this.SaveUserinfo(ls.get('USER_INFO'))
        this.completed = true
    },
    // 登录
    async Login(form){
        // 登录换取凭证
        const resp = await Api.Account.Login.Do(form)
        ls.set('USER_INFO',resp)
        ls.set('TOKEN',resp.token)
        // this.completed = false
    	return this.Init()
    },
    // 退出登录
    Logout(){
        // await Api.Common.AppObjProcess.Do({"functionName":"appLogoff","contentData":""})
        const configStore = useConfigStore()
        const form = ls.get('LOGIN_FORM')||null
        ls.clear()
        if (form) {
            ls.set('LOGIN_FORM', form)
        }
        router.push({
            path: '/login'
        })
        ls.remove('STORE_LIST')
        ls.remove('CONREOL_AUT')
        ls.remove('REPORT_TIME')
        configStore.isLoad.state = 0
        console.log(configStore.isLoad);
    },
    SaveUserinfo(userinfo) {
        this.user_info = userinfo;
    },
    SetBubblePosition(position) {
        this.bubblePosition = position;
    },
    // pinia-plugin-persist
    setReportTime(startTime,endTime) {
        this.orgReportTime.startTime = startTime;
        this.orgReportTime.endTime = endTime;
        ls.set('REPORT_TIME',this.orgReportTime)
    }
}